package com.useeinfo.oa.open.mobile;

import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.action.BaseAction;
import com.useeinfo.framework.sugar.tools.DateTimeUtils;
import com.useeinfo.oa.common.utils.ResultUtil.ResultDto;
import com.useeinfo.oa.common.utils.ResultUtil.ResultUtil;
import com.useeinfo.oa.modules.security.biz.SystemUserBiz;
import com.useeinfo.oa.modules.security.entity.SystemUser;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @program: YiYouFuPlatform
 * @author: Taogq
 * @createDate: 2018-12-20
 * @createTime: 15:12:07
 * 手机登录接口
 */
@RestController
@RequestMapping("/api/mobile/manage/user")
public class UserLoginManageAction extends BaseAction {

    @Autowired
    private SystemUserBiz systemUserBiz;
    @Autowired
    private HttpServletRequest request;

    public static final String HTTP_HEADER_AUTHORIZATION = "Authorization";

    /**
     * 用户登录接口 -- 需要返回用户所有的企业
     *
     * @param userAccount
     * @param password
     * @return
     */
    @RequestMapping("/login")
    public ResultDto login(String userAccount, String password, String deviceToken, String deviceType) {
        return systemUserBiz.login(request, userAccount, password, deviceToken, deviceType);
    }

//    @RequestMapping("/logout")
//    public ResultDto logout(HttpServletRequest request){
//        return systemUserBiz.logout(request);
//    }


    /**
     * 用户设备绑定
     *
     * @param deviceToken
     * @param deviceType
     * @return
     */
    @RequestMapping("/userDeviceBinding")
    public ResultDto userDeviceBinding(String deviceToken, String deviceType,HttpServletRequest request) {
        logger.info("用户设备绑定>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        logger.info("deviceToken:{}",deviceToken);
        logger.info("deviceType:{}",deviceType);
        String uniqueid = request.getHeader(HTTP_HEADER_AUTHORIZATION);
        SystemUser systemUser=SessionKeyUtils.getSessionUser(request);
        return systemUserBiz.userDeviceBinding(systemUser,uniqueid, deviceToken, deviceType);
    }

    /**
     * 用户登录接口 通过uniqueid登录 -- 需要返回用户所有的企业
     *
     * @param uniqueid
     * @return
     */
    @RequestMapping("/loginByUniqueid")
    public ResultDto login(String uniqueid) {
        return systemUserBiz.loginByUnique(uniqueid, request);
    }


    /**
     * 用户退出接口
     *
     * @return
     */
    @RequestMapping("/logout")
    public ResultDto logout() {
//        request.getSession().removeAttribute("LoginUser");
//        return ResultUtil.success();
        return systemUserBiz.logout(request);
    }

    //获取服务器时间
    @RequestMapping("/getServerTime")
    @ResponseBody
    public ResultDto getServerTime(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("time",DateTimeUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        return ResultUtil.success(jsonObject);
    }

}
